package com.zhida.smartcontrol.dialog.tdkgdialog;

import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TdkgDialogPresenter extends BasePresenterImpl<TdkgDialogContract.View> implements TdkgDialogContract.Presenter {

    @Override
    public void initSwitchNum(String switchType) {
        // 设置集控器通道状态  // 0关闭， 1开启
        int size = getSize(switchType);
        List<SwitchBean> dataList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            dataList.add(new SwitchBean(getName(i), false));
        }
        mView.fillSwitch(dataList);
    }

    @Override
    public void openClose(List<LampBean> codeList, String type) {
        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                list.add(lampBean.getCode());
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }

        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        params.put("type", type);// 1：开启（灯） 0：关闭（灯）
        mKalleCenter.postJson(Api.OPEN_CLOSE_SWITCH, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
                mView.onSuccess();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    private String getName(int i) {
        String name;
        switch (i) {
            case 0:
                name = "一";
                break;
            case 1:
                name = "二";
                break;
            case 2:
                name = "三";
                break;
            case 3:
                name = "四";
                break;
            case 4:
                name = "五";
                break;
            case 5:
                name = "六";
                break;
            case 6:
                name = "七";
                break;
            default:
                name = "八";
                break;
        }
        return "通道" + name;
    }

    private int getSize(String switchType) {
        int type = Integer.parseInt(switchType);
        int size;
        switch (type) {
            case Const.NB_SINGLE:
            case Const.ZIG_BEE_SINGLE:
                size = 1;
                break;
            case Const.NB_DOUBLE:
                size = 2;
                break;
            case Const.SWITCH:
                size = 8;
                break;
            default:
                size = 4;
                break;
        }
        return size;
    }

}
